<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">
    <el-form-item label="告警数量" prop="alarmStatus">
      <el-input v-model="dataForm.alarmStatus" placeholder="告警数量" type="number"></el-input>
    </el-form-item>
    <el-form-item label="告警设备id " prop="did">
      <el-input v-model="dataForm.did" placeholder="告警设备id "></el-input>
    </el-form-item>
    <el-form-item label="设备是否初始化" prop="init">
      <el-select v-model="dataForm.init" clearable placeholder="设备初始化">
        <el-option
          v-for="item in inits"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="设备状态描述" prop="label">
      <el-input v-model="dataForm.label" placeholder="如：虚拟设备/设备文件加载失败/设备未完成初始化/设备通讯异常."></el-input>
    </el-form-item>
    <el-form-item label="配置文件是否加载" prop="loadFlag">
      <el-select v-model="dataForm.loadFlag" clearable placeholder="配置文件是否加载">
        <el-option
          v-for="item in loadFlags"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="设备型号名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="设备型号名称"></el-input>
    </el-form-item>
    <el-form-item label="设备状态" prop="status">
      <el-select v-model="dataForm.status" clearable placeholder="设备状态">
        <el-option
          v-for="item in statuses"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否为虚拟设备" prop="virflag">
      <el-select v-model="dataForm.virflag" clearable placeholder="1/虚拟；0/真实">
        <el-option
          v-for="item in virflags"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/cdcdevice'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        inits: [{
          value: '0',
          label: '否'
        }, {
          value: '11',
          label: '是'
        }],
        loadFlags: [{
          value: '0',
          label: '失败'
        }, {
          value: '1',
          label: '成功'
        }],
        virflags: [{
          value: '1',
          label: '虚拟'
        }, {
          value: '0',
          label: '真实'
        }],
        //（0：未开始  -2：设备文件加载失败  -1：获取设备状态失败，未找到设备  1：设备未完成初始化
        // 2：设备通道打开失败,正在重新开启   3：设备通讯异常  4：设备通讯正常）
        statuses: [{
          value: '0',
          label: '未开始'
        }, {
          value: '-2',
          label: '设备文件加载失败'
        }, {
          value: '-1',
          label: '获取设备状态失败，未找到设备'
        }, {
          value: '1',
          label: '设备未完成初始化'
        }, {
          value: '2',
          label: '设备通道打开失败,正在重新开启'
        }, {
          value: '3',
          label: '设备通讯异常'
        }, {
          value: '4',
          label: '设备通讯正常'
        }
        ],
        dataForm: {
          id: 0,
          alarmStatus: '',

          did: '',

          init: '',

          label: '',

          loadFlag: '',

          name: '',

          status: '',

          virflag: '',

          createTime: '',

          updateTime: '',

          updateBy: '',

          delFlag: '',

        },
        dataRule: {
          alarmStatus: [
            { required: true, message: '告警数量不能为空', trigger: 'blur' }
          ],

          did: [
            { required: true, message: '告警设备id 不能为空', trigger: 'blur' }
          ],

          init: [
            { required: true, message: '设备初始化是否初始化（0：未初始化  11：初始化成功；虚拟设备无法进行初始化则为0）不能为空', trigger: 'blur' }
          ],

          label: [
            { required: true, message: '设备状态描述，如：虚拟设备/设备文件加载失败/设备未完成初始化/设备通讯异常.不能为空', trigger: 'blur' }
          ],

          loadFlag: [
            { required: true, message: '配置文件是否加载，1/成功；0/失败不能为空', trigger: 'blur' }
          ],

          name: [
            { required: true, message: '设备型号名称不能为空', trigger: 'blur' }
          ],

          status: [
            { required: true, message: '设备状态（0：未开始  -2：设备文件加载失败  -1：获取设备状态失败，未找到设备  1：设备未完成初始化  2：设备通道打开失败,正在重新开启   3：设备通讯异常  4：设备通讯正常）不能为空', trigger: 'blur' }
          ],

          virflag: [
            { required: true, message: '是否为虚拟设备，1/虚拟；0/真实不能为空', trigger: 'blur' }
          ],

          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],

          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],

          updateBy: [
            { required: true, message: '操作人不能为空', trigger: 'blur' }
          ],

          delFlag: [
            { required: true, message: '0 正常 1或者其他删除不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
